﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Ace;
using Ace.Application.Common;
using Ace.Application.Wiki;
using Ace.Entity.Wiki;
using Ace.IdStrategy;
using Ace.Web.Mvc;
using Ace.Web.Mvc.Authorization;
using Ace.Web.Mvc.Models;
using Chloe.Admin.Common;
using Chloe.Admin.Common.Tree;
using Microsoft.AspNetCore.Mvc;

namespace Chloe.Admin.Areas.Wiki.Controllers
{

    [Area(AreaNames.Wiki)]
    [Permission("wiki.promotion")]
    public class PromotionController : WebController<IPromotionService>
    {
        public IActionResult Index()
        {
            IPro_SortService SortService = this.CreateService<IPro_SortService>();
            List<Pro_Sort> SortList = SortService.GetList("", "", 0);
            ViewBag.SortList = SortList;

            return View();
        }

        public ActionResult Models(Pagination pagination, string keyword="", int ST=-1)
        { 

            PagedData<PromotionInfo> pagedData = this.Service.GetPromotionInfoList(pagination, keyword, ST);
            return this.SuccessData(pagedData);
        }

        public ActionResult GetProductList(string ProSortID)
        {
            List<Product> list = this.CreateService<IProductService>().GetList(0, ProSortID, "");
            return this.SuccessData(list);
        }

        public ActionResult DeletePromotionItem(string PromotionId,string ProductID)
        {
            this.CreateService<IPromotionItemService>().Delete(PromotionId, ProductID);
            return this.SuccessMsg();
        }

        public ActionResult GetPromotionItemList(string PromotionId)
        {
            List<PromotionItemInfo> list = this.CreateService<IPromotionItemService>().GetListInfo(PromotionId);
            return this.SuccessData(list);
        }


        [Permission("wiki.promotion.delete")]
        [HttpPost]
        public ActionResult Delete(string id)
        { 
            this.Service.Delete(id);
            return this.SuccessMsg("删除成功");
        }
         
 


        [Permission("wiki.promotion.edit")]
        [HttpPost]
        public ActionResult Edit(UpdatePromotionInput modle,string ids)
        {  
            if(!string.IsNullOrEmpty(ids))
            {
                if(ids.IndexOf(';')>-1)
                {
                    string[] arr = ids.Split(';');
                    foreach(var s in arr)
                    {
                        this.CreateService<IPromotionItemService>().Add(new PromotionItem { ProductID = s, PromotionId = modle.Id });
                    }
                }
                else
                {
                    this.CreateService<IPromotionItemService>().Add(new PromotionItem { ProductID = ids, PromotionId = modle.Id });
                }
            }
            this.Service.Update(modle);
            return this.SuccessMsg("编辑成功");
        }

        [Permission("wiki.promotion.add")]
        [HttpPost]
        public ActionResult Add(AddPromotionInput modle, string ids)
        {
            Promotion info= this.Service.Add(modle);
            if (!string.IsNullOrEmpty(ids))
            {
                if (ids.IndexOf(';') > -1)
                {
                    string[] arr = ids.Split(';');
                    foreach (var s in arr)
                    {
                        this.CreateService<IPromotionItemService>().Add(new PromotionItem { ProductID = s, PromotionId = info.Id });
                    }
                }
                else
                {
                    this.CreateService<IPromotionItemService>().Add(new PromotionItem { ProductID = ids, PromotionId = info.Id });
                }
            }
            return this.SuccessMsg("添加成功");
        }

    }
}